//
//  MetalSL.metal
//  ZSceneDemo
//
//  Created by 张鑫 on 2020/9/15.
//  Copyright © 2020 www.lionaitech.com. All rights reserved.
//

#include <metal_stdlib>
using namespace metal;

#include "ShaderTypes.h"

//与swift中的对应
//typedef struct
//{
//    float4 position;
//    float3 color;
//    float2 texture;
//} VertexIn;


typedef struct
{
    float4 position [[position]];
    float3 color;
    float2 texture;
}VertexOut;


// vertexArray 后面的 buff(0) 代表去后面配置的 index 为 0 的 MTLBuffer 资源
// vid 代表着进入的顶点的 id 即顺序。
vertex VertexOut myVertexShader(unsigned int vid  [[vertex_id]],
                                const device VertexIn* vertexArray [[buffer(0)]],
                                const device Viewport& viewport [[buffer(1)]]
                                ){
    
    VertexOut verOut;
    
    float2 xy = vertexArray[vid].position.xy;
//    xy *= viewport.scale;
    
    float2 size = float2(viewport.viewportSize);
    
    verOut.position.xy = xy / size * 2.0;
    verOut.position.zw = vertexArray[vid].position.zw;
    
    verOut.texture = vertexArray[vid].texture;
    verOut.color = vertexArray[vid].color;
    return verOut;
    
}

// 纹理展示使用
fragment float4 myFragmentShader(VertexOut vertexIn [[stage_in]],
                                 texture2d<float,access::sample> inputImage [[ texture(0) ]],
                                 sampler textureSampler [[sampler(0)]]
                                 ) {
//texture2d<half> colorTexture [[ texture(0) ]]
//    constexpr sampler textureSampler(mag_filter::linear,
//                                     min_filter::linear);

    float4 color = inputImage.sample(textureSampler, vertexIn.texture);
    return color;

}

// 顶点着色使用
//fragment float4 myFragmentShader(VertexOut in [[stage_in]])
//{
//    return float4(in.color, 0.2);
//}
